import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GUIDoctorPatient extends JPanel {

		private JLabel welcome;
		private JPanel west;
		private JPanel panel;
		private JTextField sugar;
		private JTextField pressure;
		private JTextField weight;
                private JTextField day;
                private JTextField month;
                private JTextField year;
		private JButton update;
                
                private JTextField fName;
                private JTextField lName;
                private JTextField address;
                private JPanel center;
                private JButton save;
                
                private JButton vTable;
                private JButton vGraph;
                
		private Patient current;
                private JTabbedPane jPane;

	
		
		public GUIDoctorPatient(Patient p , JTabbedPane j)
		{
                        jPane = j;
                   
			current = p;

			welcome = new JLabel("Enter your Patient Health updates below");
			sugar = new JTextField("Blood Sugar");
			pressure = new JTextField("Blood Pressure");
			weight = new JTextField("Weight");
                        day = new JTextField("Day");
                        month = new JTextField("Month");
                        year = new JTextField("Year");
			update = new JButton("Update Health Info");
			west = new JPanel();
			west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
			west.add(sugar);
			west.add(pressure);
			west.add(weight);
                        west.add(day);
                        west.add(month);
                        west.add(year);
                        west.add(update);
                        
                        center = new JPanel();
                        center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
                        fName = new JTextField(current.getFirstName());
                        lName = new JTextField(current.getLastName());
                        address = new JTextField(current.getAddress());
                        save = new JButton("Update Profile Info");
                        center.add(fName);
                        center.add(lName);
                        center.add(address);
                        center.add(save);
                        
                        vTable = new JButton("View Table");
                        
                        
			panel = new JPanel();
			panel.setLayout(new BorderLayout());
			panel.add(welcome, BorderLayout.NORTH);
			panel.add(west, BorderLayout.WEST);
                        panel.add(center, BorderLayout.CENTER);
			panel.add(vTable, BorderLayout.EAST);
			
			this.add(panel);
			
			UpdateListener listener = new UpdateListener();
			update.addActionListener(listener);
                        
                        SaveListener sl = new SaveListener();
                        save.addActionListener(sl);
                        
                        TableListener tl = new TableListener();
                        vTable.addActionListener(tl);
		}
		
		private class UpdateListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				String s = sugar.getText();
                                String p= pressure.getText();
                                String w= weight.getText();
                                String d= day.getText();
                                String m= month.getText();
                                String y= year.getText();
				
                                boolean exceptionoccurred = false;
                                
                                try
                                {
                                double sug= Double.parseDouble(s);
                                double pres = Double.parseDouble(p);
                                double weig = Double.parseDouble(w);
                                int da = Integer.parseInt(d);
                                int mon = Integer.parseInt(m);
                                int ye = Integer.parseInt(y);
                                
                                }
                                catch(Exception ex)
                                {
                                    exceptionoccurred = true;
                                    welcome.setText("Some information is incorrect.");
                                }
                                
                                if(!exceptionoccurred)
                                {
                                    while(jPane.getTabCount() > 4)
                                    {
                                        jPane.remove(4);
                                    }
                                    current.addUpdate(s,p,w,d,m,y);   
                                    welcome.setText("Information updated!");
                                }
			}
		}
                
                private class SaveListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
                                String f = fName.getText();
                                String l= lName.getText();
                                String a= address.getText();
				
                               current.setAddress(a);
                               current.setLastName(l);
                               current.setFirstName(f);
                               
                               if(jPane.getComponent(2) instanceof GUIDoctor)
                               {
                                   ((GUIDoctor)(jPane.getComponent(2))).updatefull();
                               }
				
			}
		}
                
                 private class TableListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
                            while(jPane.getTabCount() > 4)
                            {
                                jPane.remove(4);
                            }
                                jPane.add("Table", new GUITable(current));
			}
		}
	}